﻿#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace rbtree
{
	enum Colour
	{
		RED,
		BLACK
	};

template<class T>
class RBTreeNode
{
public:
	typedef RBTreeNode<T> Node;
	RBTreeNode(T& data)
		:_data(data)
	{
	}
	T _data;
	Node* _left = nullptr;
	Node* _right = nullptr;
	Node* _parent = nullptr;
	Colour _col;
};
template<class T,class Ptr, class Ref>
struct RBTreeIterator
{
	using Node = RBTreeNode<T>;
	RBTreeIterator(Node* it,Node* root)
		:_it(it)
		,_root(root)
	{}
	RBTreeIterator<T, Ptr, Ref>& operator++()
	{
		Node* cur = _it;
		Node* parent = _it->_parent;
		if (cur->_right)
		{
			while (cur->_left)
			{
				cur = cur->_left;
			}
			_it = cur;
		}
		else
		{
			while (parent && cur == parent->_right)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_it = parent;
		}
		return *this;
	}

	RBTreeIterator<T, Ptr, Ref>& operator--()
	{
		if (_it == nullptr) // end()
		{
			// --end()，特殊处理，⾛到中序最后⼀个结点，整棵树的最右结点
			Node* rightMost = _root;
			while (rightMost && rightMost->_right)
			{
				rightMost = rightMost->_right;
			}
			_it = rightMost;
		}
		else if (_it->_left)
		{
			// 左⼦树不为空，中序左⼦树最后⼀个
			Node* rightMost = _it->_left;
			while (rightMost->_right)
			{
				rightMost = rightMost->_right;
			}
			_it = rightMost;
		}
		else
		{
			// 孩⼦是⽗亲右的那个祖先
			Node* cur = _it;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_left)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_it = parent;

		}
		return *this;
	}
	Ptr operator->()
	{
		return &(_it->_data);
	}
	RBTreeNode<T> operator*()
	{
		return _it->_data;
	}
	bool operator!=(RBTreeIterator<T,Ptr,Ref> rb)
	{
		return rb._it != rb._it;
	}
	bool operator==(RBTreeIterator<T, Ptr, Ref> rb)
	{
		return rb._it == rb._it;
	}

	Node* _it;
	Node* _root;
};
template<class K, class T,class KeyOfT>
class RBTree
{
	using Node = RBTreeNode<T>;
	using Iterator = RBTreeIterator<T,T*,T&>;
	typedef RBTreeIterator<T, const T&, const T*> ConstIterator;
public:
	Iterator Begin()
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return Iterator(cur,_root);
	}
	Iterator End()
	{
		return Iterator(nullptr,_root);
	}

	ConstIterator Begin() const
	{
		Node* leftMost = _root;
		while (leftMost && leftMost->_left)
		{
			leftMost = leftMost->_left;
		}
		return ConstIterator(leftMost, _root);
	}
	ConstIterator End() const
	{
		return ConstIterator(nullptr, _root);
	}


	bool Insert(const T& data)
	{
		//树为空
		if (_root == nullptr)
		{
			_root = new Node(data);
			_root->_col = BLACK;
			return true;
		}

		Node* cur = _root;
		Node* parent = nullptr;
		KeyOfT kof;
		while (cur)
		{
			if (kof(_root->_data) < kof(data))
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (kof(data) < kof(_root->_data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(data);
		if (kof(_root->_data) < kof(data))
		{
			cur->_parent = parent;
			parent->_right = cur;
		}
		else
		{
			cur->_parent = parent;
			parent->_left = cur;
		}
		cur->_col = RED;

		
		while (parent && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			if (grandfather->_left == parent)
			{
				Node* uncle = grandfather->_right;

				//    g
				//  p   u
				// c 
				//叔叔是红色，使用变色
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_left)
					{
						//单旋变色
						RotateR(grandfather);
						parent->_col =BLACK;
						grandfather->_col = RED;
					}
					else if (cur == parent->_right)
					{
						//双旋变色
						RotateL(parent);
						RotateR(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						assert(false);
					}
					break;
				}
				
			}
			else
			{
				Node* uncle = grandfather->_left;
				//    g
				//  p   u
				// c 
				//叔叔是红色，使用变色
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_right)
					{
						//单旋变色
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else if (cur == parent->_left)
					{
						//双旋变色
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						assert(false);
					}
					break;
				}
				
			}
		}
		_root->_col =  BLACK;
		return true;
	}
	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* pParent = parent->_parent;
		parent->_left = subL->_right;
		if (subL->_right)
			subL->_right->_parent = parent;

		subL->_right = parent;
		parent->_parent = subL;
		subL->_parent = pParent;
		if (pParent && pParent->_left == parent)
		{
			pParent->_left = subL;
		}
		else if(pParent && pParent->_right == parent)
		{
			pParent->_right = subL;
		}
	}
	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* pParent = parent->_parent;

		parent->_right = subR->_left;
		if (subR->_left)
			subR->_left->_parent = parent;
		
		subR->_left = parent;
		subR->_parent = pParent;
		parent->_parent = subR;
		if (pParent && pParent->_left == parent)
		{
			pParent->_left = subR;
		}
		else if(pParent && pParent->_right == parent)
		{
			pParent->_right = subR;
		}
	}

	int Size()
	{
		return _Size(_root);
	}
	int Height()
	{
		return _Height(_root);
	}
	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}
	
private:
	
	
	int _Height(Node* root)
	{
		if (root == nullptr)return 0;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}
	int _Size(Node* root)
	{
		if (root == nullptr)return 0;
		return _Size(root->_left) + _Size(root->_right) + 1;
	}
	
	Node* _root = nullptr;
};
}